package com.wanxi.udp.demo3组播发送数据给多个接收端;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/**
 * 组播消息接收端
 */
public class UDPMulticastReceive2 {
    public static void main(String[] args) throws IOException {
        String id = "刘金祺";
        System.out.println(id + "组播接收端启动..");
        //1 创建组播socket，这里和单播是不一样的。
        MulticastSocket multicastSocket = new MulticastSocket(10000);
        // 2 创建接收的数据包
        DatagramPacket datagramPacket = new DatagramPacket(new byte[1024], 1024);

        //3 把当前计算机绑定一个组播地址,表示添加到这一组中.
        multicastSocket.joinGroup(InetAddress.getByName("224.0.3.3"));
        //4 接收消息包
        multicastSocket.receive(datagramPacket);

        String message = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
        System.out.println(new String("收到消息:" + message));
    }
}
